package com.situ.mall.function.optional;

import com.situ.mall.function.pojo.Banji;
import com.situ.mall.function.pojo.Student;
import org.junit.jupiter.api.Test;

import javax.swing.text.html.Option;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class Demo {
    @Test
    public void test1() {
        Student student = getStudent();
        if (student != null) {
            System.out.println(student.getName());
            String name = student.getName();
            if (name != null) {
                System.out.println(name.length());
            }
        }
    }

    @Test
    public void test2() {
        Student student = getStudent();
        Optional<Student> studentOptional = Optional.ofNullable(student);
        //System.out.println(studentOptional.get());
        studentOptional.ifPresent(stu -> System.out.println(stu.getName()));
    }

    @Test
    public void test55() {
        Optional<Student> studentOptional = getStudentOptional();
        studentOptional.ifPresent(student -> System.out.println(student.getName()));
    }

    @Test
    public void test4() {
        Optional<Student> studentOptional = getStudentOptional();
        //java.util.NoSuchElementException: No value present
        //Student student1 = studentOptional.get();

        //Student student2 = studentOptional.orElseGet(() -> new Student(2, "lisi", 23));
        //System.out.println(student2.getName());

        try {
            Student student3 = studentOptional.orElseThrow(() -> new RuntimeException("student为空"));
            System.out.println(student3.getName());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }

    }

    @Test
    public void test7() {
        Optional<Student> studentOptional = getStudentOptional();
        studentOptional.filter(student -> student.getAge() > 18)
                .ifPresent(student -> System.out.println(student.getName()));
    }

    @Test
    public void test8() {
        Optional<Banji> banjiOptional = getBanjiOptional();
        Optional<List<Student>> studentsOptional = banjiOptional.flatMap(banji -> Optional.ofNullable(banji.getStudents()));

        studentsOptional.ifPresent(students -> System.out.println(students.size()));
    }

    private Optional<Student> getStudentOptional() {
        Student student = new Student(1, "zhangsan", 12);
        return Optional.ofNullable(student);
    }

    private Student getStudent() {
        Student student = new Student(1, "zhangsan", 23);
        return null;
    }

    public Optional<Banji> getBanjiOptional() {
        Banji banji = new Banji(1, "Java230701", null);

        List<Student> students = new ArrayList<>();
        students.add(new Student(1, "张三1", 23));
        students.add(new Student(2, "张三2", 33));
        students.add(new Student(3, "张三3", 16));

        banji.setStudents(students);
        return Optional.ofNullable(banji);
    }
}
